//
//  MHVersion.m
//  FootPrint
//
//  Created by 秦 东方 on 4/23/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "MHVersion.h"

@implementation MHVersion


@synthesize version;

+ (MHVersion *)mainBundleVersion {
	NSString* current = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    return [[[self alloc] initWithVersion:current] autorelease];
}

+ (MHVersion *)osVersion {
    NSString *os = [[UIDevice currentDevice] systemVersion];
    return [[[self alloc] initWithVersion:os] autorelease];
}

- (id)init {
    return [self initWithVersion:nil];
}

- (id)initWithVersion:(NSString *)v {
    if(self = [super init]) {
        version = [v retain];
    }
    return self;
}

- (void)dealloc
{
    [version release];
    [super dealloc];
}

- (BOOL)isNewerThan:(MHVersion *)ver {
    if(!ver)
        return YES;
	NSScanner *scanner1 = [[[NSScanner alloc] initWithString:version] autorelease];
	NSScanner *scanner2 = [[[NSScanner alloc] initWithString:ver.version] autorelease];
	BOOL result = NO;
	for(int i = 0; i < 4; i++) {
		NSInteger i1 = 0, i2 = 0;
		[scanner1 scanInteger:&i1];
		[scanner2 scanInteger:&i2];
		if(i1 > i2) {
			result = YES;
			break;
		} else if(i1 < i2) {
			result = NO;
			break;
		}
		[scanner1 scanString:@"." intoString:nil];
		[scanner2 scanString:@"." intoString:nil];
	}
	return result;
}

- (BOOL)isOlderThan:(MHVersion *)ver {
    if(!ver)
        return NO;
    return ![ver isNewerThan:self];
}

@end
